<?php

namespace App\Models;

use Illuminate\Support\Facades\Redis;

/**
 * Class Blog
 * @property integer id
 * @property string type 类型
 * @property integer type_id 类型ID
 * @property integer user_id 用户ID
 * @property string content 评论内容
 * @property integer level 评论星级
 * @property integer other_id 其它ID
 * @property integer parent_id 父级ID 默认为0
 * @property integer reply_id 回复ID 默认为0
 * @property boolean anonymous 是否匿名
 * @property Comment replyObj 是否匿名
 */
class Comment extends ApplicationModel
{
    protected $fillable = [
        'type_id',
        'type',
        'user_id',
        'content',
        'level',
        'anonymous',
        'other_id',
        'parent_id',
        'reply_id',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function purchase()
    {
        return $this->belongsTo(Purchase::class, 'other_id')->withTrashed();
    }

    public function items()
    {
        return $this->hasMany(Comment::class,'parent_id');
    }

    public function replyObj()
    {
        return $this->belongsTo(Comment::class,'reply_id')->withTrashed();
    }

    static function get_filter_key()
    {
        $key = "commentfilter";
        return $key;
    }
    static function comment_filter($comment)
    {
        $key = self::get_filter_key();
        $filters = Redis::get($key);
        $num = 0;
        $filters = explode("\r\n", $filters);
        $sorttype = create_function('$a, $b', 'return(strLen($a) < strLen($b));');
        usort($filters, $sorttype);
        if (!is_array($filters)) {
            return true;
        }
        if (count($filters) == 0) {
            return true;
        }
        //根据数组元素数量执行for循环
        for ($i = 0; $i < count($filters); $i++) {
            if (trim($filters[$i]) == '') {
                continue;
            }
            if (preg_match("/(" . $filters[$i] . ")/uis", $comment)) {
                $num++;
            }
        }
        if ($num > 0) {
            return false;
        }
        return true;
    }



    /**
     * 回复的推送
     * @param $content
     * @param $url
     */
    public function send_reply_msg($content, $url){
        //发送推送
        $message = new Message();
        $message->user_id = $this->replyObj->user_id;
        $message->content = $content;
        $message->icon = Message::ICON;
        $message->url_schema = $url;
        $message->type_id = $this->id;
        $message->save();

        $message->pushMsg($this->replyObj->user_id, $content, "{'url':'" . $url . "','_NOTIFICATION_BAR_STYLE_':'2'}");
    }


    public function tranTime($time) {
        $time = strtotime($time);
        $rtime = date("m-d H:i",$time);
        $htime = date("H:i",$time);
        $time = time() - $time;
        if ($time < 60) {
            $str = '刚刚';
        }
        elseif ($time < 60 * 60) {
            $min = floor($time/60);
            $str = $min.'分钟前';
        }
        elseif ($time < 60 * 60 * 24) {
            $h = floor($time/(60*60));
            $str = $h.'小时前 '.$htime;
        }
        elseif ($time < 60 * 60 * 24 * 3) {
            $d = floor($time/(60*60*24));
            if($d==1)
                $str = '昨天 '.$rtime;
            else
                $str = '前天 '.$rtime;
        }
        else {
            $str = $rtime;
        }
        return $str;
    }

}
